package com.UBC417.termproject.Servlet;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.UBC417.termproject.Data.User;

@SuppressWarnings("serial")
public class UpdateDatabase extends HttpServlet {
	private static final Logger log = Logger.getLogger(SubscribeServlet.class
			.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		User.updateAuction();
		
		HttpSession session = req.getSession();
		String userId = (String) session.getAttribute("id");

		String type = User.getUserType(userId);
		String forwardTo = "/account.jsp";
		if (type.equals("Seller")) {
			forwardTo = "/seller.jsp";
		} else {
			forwardTo = "/buyer.jsp";
		}		
		
		log.info("user: "+ userId);
		log.info ("update database forward to: "+ forwardTo);

		// redirect to user page
		ServletContext sc = getServletContext();
		RequestDispatcher rd = sc.getRequestDispatcher(forwardTo);
		rd.forward(req, resp);
	}

}
